package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.LimitCon;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysAboutme;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysNoticeService;
import com.ruoyi.system.service.ISysNoticeServiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告-客服Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/system/service")
public class SysNoticeServiceController extends BaseController
{
    @Autowired
    private ISysNoticeServiceService sysNoticeServiceService;

    /**
     * 查询公告-客服列表
     */
    @PreAuthorize("@ss.hasPermi('system:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNoticeService sysNoticeService)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysNoticeService.setOwnerid("1");
        }else{
            sysNoticeService.setOwnerid(user.getUserId()+"");
        }

        startPage();
        List<SysNoticeService> list = sysNoticeServiceService.selectSysNoticeServiceList(sysNoticeService);
        return getDataTable(list);
    }

    /**
     * 导出公告-客服列表
     */
    @PreAuthorize("@ss.hasPermi('system:service:export')")
    @Log(title = "公告-客服", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNoticeService sysNoticeService)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysNoticeService.setOwnerid("1");
        }else{
            sysNoticeService.setOwnerid(user.getUserId()+"");
        }
        List<SysNoticeService> list = sysNoticeServiceService.selectSysNoticeServiceList(sysNoticeService);
        ExcelUtil<SysNoticeService> util = new ExcelUtil<SysNoticeService>(SysNoticeService.class);
        util.exportExcel(response, list, "公告-客服数据");
    }

    /**
     * 获取公告-客服详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:service:query')")
    @GetMapping(value = "/{noticeid}")
    public AjaxResult getInfo(@PathVariable("noticeid") Long noticeid)
    {
        return success(sysNoticeServiceService.selectSysNoticeServiceByNoticeid(noticeid));
    }

    /**
     * 新增公告-客服
     */
    @PreAuthorize("@ss.hasPermi('system:service:add')")
    @Log(title = "公告-客服", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNoticeService sysNoticeService)

    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysNoticeService querySysNoticeService=new SysNoticeService();
        if (user.isAdmin())
        {
            sysNoticeService.setOwnerid("1");
            querySysNoticeService.setOwnerid("1");
        }else{
            sysNoticeService.setOwnerid(user.getUserId()+"");
            querySysNoticeService.setOwnerid(user.getUserId()+"");
        }
        //如果新增的是 启用状态，需要查看目前启用状态的有几条
        if (sysNoticeService.getStatus() == 0) {
            querySysNoticeService.setStatus(0);
            List result = sysNoticeServiceService.selectSysNoticeServiceList(querySysNoticeService);
            if (result != null && result.size() > 0) {
                return AjaxResult.error("已有启用状态的条目，只允许一条启用状态数据");
            }
        }
        List result = sysNoticeServiceService.selectSysNoticeServiceList(querySysNoticeService);
        if (result != null && result.size() > LimitCon.MaxRowSize-1) {
            return AjaxResult.error("已超出最多条目数，只允许存在" + LimitCon.MaxRowSize +"条数据");
        }
        return toAjax(sysNoticeServiceService.insertSysNoticeService(sysNoticeService));
    }

    /**
     * 修改公告-客服
     */
    @PreAuthorize("@ss.hasPermi('system:service:edit')")
    @Log(title = "公告-客服", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNoticeService sysNoticeService)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysNoticeService querySysNoticeService=new SysNoticeService();
        if (user.isAdmin())
        {
            querySysNoticeService.setOwnerid("1");
        }else{
            querySysNoticeService.setOwnerid(user.getUserId()+"");
        }
        //如果新增的是 启用状态，需要查看目前启用状态的有几条
        if (sysNoticeService.getStatus() == 0) {
            querySysNoticeService.setStatus(0);
            List<SysNoticeService> result = sysNoticeServiceService.selectSysNoticeServiceList(querySysNoticeService);

            if (result != null && result.size() > 0) {
                boolean isself=false;
                for (int i=0;i<result.size();i++){
                    SysNoticeService ee=result.get(i);
                    if (ee.getNoticeid().equals(sysNoticeService.getNoticeid())){
                        isself=true;
                        break;
                    }
                }
                if(!isself){
                    return AjaxResult.error("已有启用状态的条目，只允许一条启用状态数据");
                }
            }
        }
        return toAjax(sysNoticeServiceService.updateSysNoticeService(sysNoticeService));
    }

    /**
     * 删除公告-客服
     */
    @PreAuthorize("@ss.hasPermi('system:service:remove')")
    @Log(title = "公告-客服", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeids}")
    public AjaxResult remove(@PathVariable Long[] noticeids)
    {
        return toAjax(sysNoticeServiceService.deleteSysNoticeServiceByNoticeids(noticeids));
    }
}
